<?php

namespace app\admin\controller;
use think\Db;
use my\Security;

class WhiteList extends Base {

    public function _initialize() {
        parent::_initialize();
    }
    //设置白名单
    public function add(){
        if(input('post.')){
            $data = input('post.');
            $data['driver_str']     = preg_replace("/(\n)|(\s)|(\t)|(\')|(')|(，)/" ,',' ,$data['driver_str']);      //快车
            $data['fr_driver_str']  = preg_replace("/(\n)|(\s)|(\t)|(\')|(')|(，)/" ,',' ,$data['fr_driver_str']);   //顺风车
            $data['uid_str']        = preg_replace("/(\n)|(\s)|(\t)|(\')|(')|(，)/" ,',' ,$data['uid_str']);         //用户
            //快车
            $data['driver_str']     = explode(',',$data['driver_str']);
            $driver_base = [];
            foreach ($data['driver_str'] as $key=>$value){
                $driver_id = Db::name('driver_base')->where(['driver_phone'=>Security::encrypt($value)])->value('driver_id');
                if(!empty($driver_id)){
                    $driver_base[] = $driver_id;
                }
            }
            $data['driver_str'] = !empty($driver_base)?implode(',',$driver_base):'';
            //顺风车
            $data['fr_driver_str']   = explode(',',$data['fr_driver_str']);
            $fr_driver = [];
            foreach ($data['fr_driver_str'] as $key=>$value){
                $fr_driver_id = Db::name('user')->where(['user_phone'=>Security::encrypt($value),'is_fr'=>1,'is_del'=>1])->value('uid');
                if(!empty($fr_driver_id)){
                    $fr_driver[] = $fr_driver_id;
                }
            }
            $data['fr_driver_str'] = !empty($fr_driver)?implode(',',$fr_driver):'';
            //用户
            $data['uid_str']   = explode(',',$data['uid_str']);
            $user_info = [];
            foreach ($data['uid_str'] as $key=>$value){
                $user_id = Db::name('user')->where(['user_phone'=>Security::encrypt($value),'is_del'=>1])->value('uid');
                if(!empty($user_id)){
                    $user_info[] = $user_id;
                }
            }
            $data['uid_str'] = !empty($user_info)?implode(',',$user_info):'';
            //数据入库操作
            if(!empty($data['id'])){
                Db::name('white_list')->where(['id'=>$data['id']])->update(['driver_str'=>$data['driver_str'],'fr_driver_str'=>$data['fr_driver_str'],'uid_str'=>$data['uid_str']]);
            }else{
                $data['add_time'] = time();
                $data['admin_id'] = session('admin_id');
                Db::name('white_list')->insertGetId($data);
            }
            echo ajax_success('提交成功');die;
        }else{
            //查询白名单信息
            $whiteList = Db::name('white_list')->order('id desc')->find();
            if(!empty($whiteList)){
                //查询快车 - 司机手机号码
                $driver_info = Db::name('driver_base')->where(['driver_id'=>['in',$whiteList['driver_str']]])->field('driver_phone')->select();
                $driver_phone = '';
                foreach ($driver_info as $key=>$value){
                    $driver_phone .= Security::decrypt($value['driver_phone']).',';
                }
                $whiteList['driver_phone'] = rtrim($driver_phone,',');
                //查询顺风车 - 司机手机号码
                $fr_driver_info = Db::name('user')->where(['is_del'=>1,'uid'=>['in',$whiteList['fr_driver_str']]])->field('user_phone')->select();
                $fr_driver_phone = '';
                foreach ($fr_driver_info as $key=>$value){
                    $fr_driver_phone .= Security::decrypt($value['user_phone']).',';
                }
                $whiteList['fr_driver_phone'] = rtrim($fr_driver_phone,',');
                //查询用户 - 手机号码
                $user_info = Db::name('user')->where(['is_del'=>1,'uid'=>['in',$whiteList['uid_str']]])->field('user_phone')->select();
                $user_phone = '';
                foreach ($user_info as $key=>$value){
                    $user_phone .= Security::decrypt($value['user_phone']).',';
                }
                $whiteList['user_phone'] = rtrim($user_phone,',');
            }
            $this->assign('whiteList',$whiteList);
        }
        return $this->fetch();
    }
}
